package com.dms.web.controller.system;

import java.util.List;

import com.dms.system.service.ISysAreaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.dms.common.annotation.Log;
import com.dms.common.enums.BusinessType;
import com.dms.system.domain.SysPlant;
import com.dms.system.service.ISysPlantService;
import com.dms.common.core.controller.BaseController;
import com.dms.common.core.domain.AjaxResult;
import com.dms.common.utils.poi.ExcelUtil;
import com.dms.common.core.page.TableDataInfo;

/**
 * 设备Controller
 * 
 * @author xlong_zhu
 * @date 2021-03-28
 */
@Controller
@RequestMapping("/system/plant")
public class SysPlantController extends BaseController
{
    private String prefix = "system/plant";

    @Autowired
    private ISysPlantService sysPlantService;

    @Autowired
    private ISysAreaService sysAreaService;

    @RequiresPermissions("system:plant:view")
    @GetMapping()
    public String plant(Model model)
    {
        model.addAttribute("areaList", sysAreaService.selectSysAreaList(null));
        return prefix + "/plant";
    }

    /**
     * 查询设备列表
     */
    @RequiresPermissions("system:plant:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysPlant sysPlant)
    {
        startPage();
        List<SysPlant> list = sysPlantService.selectSysPlantList(sysPlant);
        return getDataTable(list);
    }

    /**
     * 导出设备列表
     */
    @RequiresPermissions("system:plant:export")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysPlant sysPlant)
    {
        List<SysPlant> list = sysPlantService.selectSysPlantList(sysPlant);
        ExcelUtil<SysPlant> util = new ExcelUtil<SysPlant>(SysPlant.class);
        return util.exportExcel(list, "plant");
    }

    /**
     * 新增设备
     */
    @GetMapping("/add")
    public String add(Model m)
    {
        m.addAttribute("areaList",sysAreaService.selectSysAreaList(null));
        return prefix + "/add";
    }

    /**
     * 新增保存设备
     */
    @RequiresPermissions("system:plant:add")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysPlant sysPlant)
    {
        return toAjax(sysPlantService.insertSysPlant(sysPlant));
    }

    /**
     * 修改设备
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysPlant sysPlant = sysPlantService.selectSysPlantById(id);
        mmap.put("sysPlant", sysPlant);
        mmap.put("areaList", sysAreaService.selectSysAreaList(null));
        return prefix + "/edit";
    }

    /**
     * 修改保存设备
     */
    @RequiresPermissions("system:plant:edit")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysPlant sysPlant)
    {
        return toAjax(sysPlantService.updateSysPlant(sysPlant));
    }

    /**
     * 删除设备
     */
    @RequiresPermissions("system:plant:remove")
    @Log(title = "设备", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysPlantService.deleteSysPlantByIds(ids));
    }
}
